<script setup>
    import {ref} from 'vue'
    import { Lock } from '@element-plus/icons-vue'
    import {ElMessage} from 'element-plus'
   const form=ref({
    old_pwd:'',
    new_pwd:'',
    re_pwd:''
   })

   const checkPassWord=(rule,value,callback)=>{
  if(value===''){
    callback(new Error('请再次确认密码'))
  }else if(value!==form.value.new_pwd){
    callback(new Error('两次输入的密码不一致'))
  }else{
    callback()
  }
}
  const rules={
    old_pwd:[
    { required: true, message: '请输入旧密码', trigger: 'blur' },//鼠标失去焦点
    { min: 5, max: 16, message: '长度为5-16位非空字符', trigger: 'blur' }
    ],
  new_pwd:[
    { required: true, message: '请输入新密码', trigger: 'blur' },//鼠标失去焦点
    { min: 5, max: 16, message: '长度为5-16位非空字符', trigger: 'blur' }
    ],
    re_pwd:[
        { validator:checkPassWord,trigger:'blur'}
    ]}
   //调用修改密码接口
   import {updatePassword} from '@/api/user.js'
   const update=async()=>{
    let result= await  updatePassword(form.value);
    ElMessage.success(result.msg?result.msg:'修改成功')
   }

   const clear=()=>{
    form.value.new_pwd='',
    form.value.old_pwd='',
    form.value.re_pwd=''
   }
</script>
<template>
    <p>重置密码</p>
    <el-form :model="form" label-width="atuo" style="max-width: 30%" :rules="rules">
    <el-form-item prop="old_pwd" >
      <el-input :prefix-icon="Lock" v-model="form.old_pwd" placeholder="旧密码"/>
    </el-form-item>
    <el-form-item prop="new_pwd">
      <el-input :prefix-icon="Lock" v-model="form.new_pwd" placeholder="新密码"/>
    </el-form-item>
    <el-form-item prop="re_pwd">
      <el-input :prefix-icon="Lock"v-model="form.re_pwd" placeholder="再次输入新密码"/>
    </el-form-item>
    <el-form-item class="id">
      <el-button type="primary" @click="update">修改</el-button>
      <el-button @click="clear">取消</el-button>
    </el-form-item>
    </el-form>
</template>

<style lang="scss" scoped>
 .id{
    margin-left: 58px;
 }
</style>